package com.abing.starry.service.impl;

import cn.hutool.json.JSONUtil;
import com.abing.sandbox.request.JudgeInfo;
import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.mapper.QuestionSubmitMapper;
import com.abing.starry.model.entity.QuestionSubmitEntity;
import com.abing.starry.model.enums.QuestionSubmitStatusEnum;
import com.abing.starry.model.request.question.SubmitRecordRequest;
import com.abing.starry.model.vo.question.QuestionSubmitVO;
import com.abing.starry.service.QuestionSubmitService;
import com.abing.starry.util.TokenUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 阿炳亿点点帅
 * @description 针对表【question_submit(题目提交)】的数据库操作Service实现
 * @createDate 2024-09-05 17:30:00
 */
@Service
public class QuestionSubmitServiceImpl extends ServiceImpl<QuestionSubmitMapper, QuestionSubmitEntity>
        implements QuestionSubmitService {

    @Override
    public IPage<QuestionSubmitVO> listQuestionSubmitByPage(SubmitRecordRequest submitRecordRequest) {
        String questionId = submitRecordRequest.getQuestionId();
        ThrowUtils.throwIf(StringUtils.isEmpty(questionId), ErrorCode.PARAMS_ERROR, "题目id不存在");

        String userId = TokenUtils.getCurrentUser().getId();
        Page<QuestionSubmitEntity> submitEntityPage = this.page(new Page<>(submitRecordRequest.getCurrent(), submitRecordRequest.getPageSize()),
                                                                Wrappers.lambdaQuery(QuestionSubmitEntity.class)
                                                                        .eq(QuestionSubmitEntity::getQuestionId, questionId)
                                                                        .eq(QuestionSubmitEntity::getUserId, userId)
                                                                        .orderByDesc(QuestionSubmitEntity::getCreateTime));

        List<QuestionSubmitEntity> pageRecords = submitEntityPage.getRecords();
        List<QuestionSubmitVO> questionSubmitVOList = pageRecords
                .stream()
                .map(submitEntity -> {
                    QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
                    questionSubmitVO.setId(submitEntity.getId());
                    Integer status = submitEntity.getStatus();
                    questionSubmitVO.setStatus(status);
                    questionSubmitVO.setLanguage(submitEntity.getLanguage());
                    questionSubmitVO.setCreateTime(submitEntity.getCreateTime());
                    questionSubmitVO.setMessage(Objects.requireNonNull(QuestionSubmitStatusEnum.of(status)).getText());
                    List<JudgeInfo> judgeInfoList = JSONUtil.toList(submitEntity.getJudgeInfo(), JudgeInfo.class);
                    Long maxTime = judgeInfoList.stream()
                                                .map(JudgeInfo::getTime)
                                                .max(Long::compareTo)
                                                .orElse(0L);
                    questionSubmitVO.setTime(maxTime);
                    // byte
                    long maxMemory = judgeInfoList.stream()
                                                  .map(JudgeInfo::getMemory)
                                                  .max(Long::compareTo)
                                                  .orElse(0L);
                    // mb
                    maxMemory = maxMemory / 1024 / 1024;
                    questionSubmitVO.setMemory(maxMemory);
                    return questionSubmitVO;
                }).collect(Collectors.toList());

        Page<QuestionSubmitVO> pageResult = new Page<>();
        BeanUtils.copyProperties(submitEntityPage, pageResult);
        pageResult.setRecords(questionSubmitVOList);

        return pageResult;
    }
}




